package com.xzg.common.push;

import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.exceptions.RequestException;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.LinkTemplate;
import com.gexin.rp.sdk.template.TransmissionTemplate;
import com.gexin.rp.sdk.template.style.Style0;
import com.xzg.common.constants.Constant;
import com.xzg.user.entity.User;

/**
 * @Author Ye
 * Date 2018-03-15 14:31
 * @Description 个推推送
 */
public class AppPush {


    public static void pushSingle(String message1, String cid) {
        IGtPush push = new IGtPush(Constant.HOST, Constant.APPKEY, Constant.MASTERSECURT);
        TransmissionTemplate template = new TransmissionTemplate();
        template.setAppkey(Constant.APPKEY);
        template.setAppId(Constant.APPID);
        template.setTransmissionType(2);
        template.setTransmissionContent(message1);

        SingleMessage message = new SingleMessage();
        message.setOffline(true);
        // 离线有效时间，单位为毫秒，可选
        message.setOfflineExpireTime(24 * 3600 * 1000);
        message.setData(template);
        // 可选，1为wifi，0为不限制网络环境。根据手机处于的网络情况，决定是否下发
        message.setPushNetWorkType(0);
        Target target = new Target();
        target.setAppId(Constant.APPID);
        target.setClientId(cid);
        //target.setAlias(Alias);
        IPushResult ret = null;
        try {
            ret = push.pushMessageToSingle(message, target);
        } catch (RequestException e) {
            e.printStackTrace();
            ret = push.pushMessageToSingle(message, target, e.getRequestId());
        }
        if (ret != null) {
            System.out.println(ret.getResponse().toString());
        } else {
            System.out.println("服务器响应常");
        }
    }

    public static void pushSingle2(String message1, String cid) {
        IGtPush push = new IGtPush(Constant.HOST, Constant.APPKEY, Constant.MASTERSECURT);
        TransmissionTemplate template = new TransmissionTemplate();
        template.setAppkey(Constant.APPKEY);
        template.setAppId(Constant.APPID);
        template.setTransmissionType(2);
        template.setTransmissionContent(message1);

        SingleMessage message = new SingleMessage();
        message.setOffline(true);
        // 离线有效时间，单位为毫秒，可选
        message.setOfflineExpireTime(24 * 3600 * 1000);
        message.setData(template);
        // 可选，1为wifi，0为不限制网络环境。根据手机处于的网络情况，决定是否下发
        message.setPushNetWorkType(0);
        Target target = new Target();
        target.setAppId(Constant.APPID);
        target.setClientId(cid);
        //target.setAlias(Alias);
        IPushResult ret = null;
        try {
            ret = push.pushMessageToSingle(message, target);
        } catch (RequestException e) {
            e.printStackTrace();
            ret = push.pushMessageToSingle(message, target, e.getRequestId());
        }
        if (ret != null) {
            System.out.println(ret.getResponse().toString());
        } else {
            System.out.println("服务器响应異常");
        }
    }

//    public static void main(String[] args) {
//        User user = new User();
//        user.setCID(Constant.CID);
//        AppPush.pushSingle("{\"title\":\"Test\",\"content\":\"喵！~\"}", user);
//        AppPush.pushSingle2("{\"title\":\"Test\",\"content\":\"喵！~\",\"payload\":\"喵！~\"}", user);
//    }


}
